package org.itboys.shop.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.seller.constant.SellerConstants;
import org.itboys.shop.entity.TouristSpot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

/**
 * 商家
 * @author weisky
 *
 */
@Service
public class TouristService extends BaseService<TouristSpot, Long>{

    private static final long serialVersionUID = 1L;
    @Resource(name = "sellerDS")
    private MongoDataSource ds;

    @Autowired
    private ResourceHolder resourceHolder;

    @Resource
    private ShopSellerService shopSellerService;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<TouristSpot> getEntityClass() {
        return TouristSpot.class;
    }

    /**
     * 根据商店名称模糊查询列表
     * @param shopName
     * @return
     */
    public List<TouristSpot> getShopListListByName(String sellerName) {
        return ds.createQuery(getEntityClass()).filter("name", AceUtils.getPattern(sellerName))
                .filter("isDeleted", SellerConstants.TYPE_ZERO).asList();
    }

    public Page<TouristSpot> sellerListByPage(Map<String, Object> param, HttpServletRequest request){
        return pageQuery(param, request);
    }

    /**
     * 保存或修改
     * @param seller
     * @param logos:商家logo
     * @param mainImgs:商家主图
     * @return
     */
    public boolean insert(TouristSpot seller, MultipartFile logos, MultipartFile mainImgs){
        try{
            String logo = null;
            String mainImg = null;

            if(logos != null && !logos.isEmpty()){
                logo = FileUtils.saveFile(logos.getInputStream(),
                        resourceHolder.getStringValue("fileUploadPath"),
                        logos.getOriginalFilename(), logos.getContentType());
                seller.setLogo(logo);
            }
            if(mainImgs != null && !mainImgs.isEmpty()){
                mainImg = FileUtils.saveFile(mainImgs.getInputStream(),
                        resourceHolder.getStringValue("fileUploadPath"),
                        mainImgs.getOriginalFilename(), mainImgs.getContentType());
                seller.setMainImg(mainImg);
            }

            if(seller.getId() != 0){//修改
                TouristSpot seller2 = getById(seller.getId());
                //设置商家的logo
                if(StringUtils.isBlank(logo)){
                    seller.setLogo(seller2.getLogo());
                }
                if(StringUtils.isBlank(mainImg)){
                    seller.setMainImg(seller2.getMainImg());
                }
                update(seller);
            }else{//新增
                save(seller);
            }
            return true;
        }catch(Exception e){
            e.printStackTrace();
            logger.error("SellerService.insert error", e);
            return false;
        }
    }

    @Override
    public long getNextId() {
        return shopSellerService.getNextId();
    }

    /**
     * 修改商家的状态
     * @param sellerId
     * @param state
     * @return
     */
    public boolean updateState(Long sellerId, int state){
        try{
            update(sellerId, "state", state);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            logger.error("SellerService.updateState error", e);
            return false;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean dele(Long id){
        try{
            delete(id);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            logger.error("SellerService.dele error", e);
            return false;
        }
    }

    public List<TouristSpot> findAllTourist(long scenicId) {
        return getMongoDataSource().createQuery(TouristSpot.class)
                .field("scenicId").equal(scenicId).asList();
    }

}
